/**
 * 
 */
package com.zy.ygCore.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zy.ygCore.util.Md5Util;
import com.zy.ygCore.util.RSAUtil3;
import com.zy.ygCore.util.SignUtil;

/** 
 *
 * @author: luochao
 * @since: 2020年2月8日  下午1:12:27
 * @history:
 */
@RestController
@RequestMapping
public class IntexController extends BaseController {

    private static CloseableHttpClient httpsclient = HttpClientBuilder.create().build();

    //测试地址, 请以开发文档为准
    @Value("${opengateway.url:}")
    private String                     url;

    /**
     * 向云马智慧开放平台发送请求（Rsa）
     *
     * @param mv
     * @return
     * @throws Exception 
     * @create  2020年2月8日 下午2:41:20 luochao
     * @history
     */
    @RequestMapping(path = "/sendPostRsa")
    public String sendPostRsa() throws Exception {
        HttpPost httpPost = new HttpPost(url);
        Map<String, String> map = new HashMap<String, String>();
        //自定义参数，项目中以实际为准
        map.put("a", "a");
        map.put("b", "你好");
        //开发者应用id,由云马开放平台提供,纯粹测试接口,不用修改此参数, 实际开发中请修改为实际值
        map.put("ymAppId", "2102341240266719233");
        String signParams = Md5Util.md5(SignUtil.getSignParams(map));
        //公钥,由云马开放平台提供,纯粹测试接口,不用修改此参数, 实际开发中请修改为实际值
        File file = ResourceUtils.getFile("classpath:rsa_public_key.pem");
        String privateKey = readFileContent(file);
        map.put("sign", RSAUtil3.encryptByPublicKey(signParams, privateKey));
        httpPost.setEntity(assembleParam(map, ContentType.APPLICATION_FORM_URLENCODED));
        CloseableHttpResponse response = httpsclient.execute(httpPost);
        //result返回为空或未返回错误信息及代表测试成功
        String result = toString(response.getEntity());
        return result;
    }

    /**
     * 向云马智慧开放平台发送请求（Md5）
     *
     * @return
     * @throws Exception 
     * @create  2020年7月16日 下午2:04:43 luochao
     * @history
     */
    @RequestMapping(path = "/sendPostMd5")
    public String sendPostMd5() throws Exception {
        HttpPost httpPost = new HttpPost(url);
        Map<String, String> map = new HashMap<String, String>();
        //自定义参数，项目中以实际为准
        map.put("a", "a");
        map.put("b", "你好");
        //开发者应用id,由云马开放平台提供,纯粹测试接口,不用修改此参数, 实际开发中请修改为实际值
        map.put("ymAppId", "2007161016309001");
        //秘钥,由云马开放平台提供,纯粹测试接口,不用修改此参数, 实际开发中请修改为实际值
        String privateKey = "VzSfzUcrVeWws";
        String sign = SignUtil.getSign(map, privateKey);
        map.put("sign", sign);
        httpPost.setEntity(assembleParam(map, ContentType.APPLICATION_FORM_URLENCODED));
        CloseableHttpResponse response = httpsclient.execute(httpPost);
        //result返回为空或未返回错误信息及代表测试成功
        String result = toString(response.getEntity());
        return result;
    }

    /**
     * 接收云马智慧开放平台的回调请求
     *
     * @param req
     * @return
     * @throws Exception 
     * @create  2020年2月18日 下午2:12:06 luochao
     * @history
     */
    @RequestMapping(path = "/receive")
    public String receive(HttpServletRequest req) throws Exception {
        Map<String, String> map = getRequestParmeter(req);
        String signParams = Md5Util.md5(SignUtil.getSignParams(map));
        //公钥,由云马开放平台提供,纯粹测试接口是否可以不用修改此参数, 实际开发中请修改为实际值
        File file = ResourceUtils.getFile("classpath:rsa_public_key.pem");
        String privateKey = readFileContent(file);
        String sign = req.getParameter("sign");
        String decryptByPrivateKey = RSAUtil3.decryptByPublicKey(sign, privateKey);
        String msg = "";
        if (signParams.equals(decryptByPrivateKey)) {
            msg = "测试成功";
        } else {
            msg = "测试失败";
        }
        return msg;
    }

}
